﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Hutech.Authentication.Library.Web.Framework;

namespace Hutech.CRM.Web.UI
{
    /// <summary>
    /// designed for flexible rendering of headings similar to ModuleTitleControl but for other palces where there is no module involved but the same rendering is desired
    /// configurable from theme.skin
    /// </summary>
    public class HeadingControl : WebControl
    {
        private string artHeader = UIHelper.ArtisteerPostMetaHeader;
        private string artHeadingCss = UIHelper.ArtPostHeader;

        private string text = string.Empty;

        public string Text
        {
            get
            {
                return text;
            }
            set
            {
                text = value;
            }
        }

        private string headingTag = "h2";

        public string HeadingTag
        {
            get
            {
                return headingTag;
            }
            set
            {
                headingTag = value;
            }
        }

        private string literalExtraMarkup = string.Empty;

        public string LiteralExtraMarkup
        {
            get
            {
                return literalExtraMarkup;
            }
            set
            {
                literalExtraMarkup = value;
            }
        }

        private bool renderArtisteer = false;

        public bool RenderArtisteer
        {
            get
            {
                return renderArtisteer;
            }
            set
            {
                renderArtisteer = value;
            }
        }

        private bool useLowerCaseArtisteerClasses = false;

        public bool UseLowerCaseArtisteerClasses
        {
            get
            {
                return useLowerCaseArtisteerClasses;
            }
            set
            {
                useLowerCaseArtisteerClasses = value;
            }
        }

        private bool useArtisteer3 = false;

        public bool UseArtisteer3
        {
            get
            {
                return useArtisteer3;
            }
            set
            {
                useArtisteer3 = value;
            }
        }

        private bool useJQueryUI = false;

        public bool UseJQueryUI
        {
            get
            {
                return useJQueryUI;
            }
            set
            {
                useJQueryUI = value;
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (HttpContext.Current == null)
            {
                writer.Write("[" + this.ID + "]");
                return;
            }
            else
            {
                if (renderArtisteer)
                {
                    if (useLowerCaseArtisteerClasses)
                    {
                        artHeader = UIHelper.ArtisteerPostMetaHeaderLower;
                        artHeadingCss = UIHelper.ArtPostHeaderLower;
                    }

                    writer.Write("<div class=\"" + artHeader + "\">\n");

                    if ((artHeader == UIHelper.ArtisteerBlockHeader) || (artHeader == UIHelper.ArtisteerBlockHeaderLower))
                    {
                        writer.Write("<div class=\"l\"></div>");
                        writer.Write("<div class=\"r\"></div>");
                        writer.Write("<div class=\"art-header-tag-icon\">");
                        if (!useArtisteer3)
                        {
                            writer.Write("<div class=\"t\">");
                        }
                    }
                }
                else
                    if (useJQueryUI)
                    {
                        writer.Write("<div class=\"ui-widget-header ui-corner-top\">");
                    }


                if (headingTag.Length > 0)
                {
                    writer.WriteBeginTag(headingTag);
                    if (useArtisteer3)
                    {
                        writer.WriteAttribute("class", artHeadingCss + " t moduletitle " + CssClass);
                    }
                    else
                    {
                        writer.WriteAttribute("class", artHeadingCss + " moduletitle " + CssClass);
                    }
                    writer.Write(HtmlTextWriter.TagRightChar);

                    writer.Write(text);

                    if (literalExtraMarkup.Length > 0)
                    {
                        writer.Write(literalExtraMarkup);
                    }

                    writer.WriteEndTag(headingTag);
                }

                if (renderArtisteer)
                {
                    writer.Write("</div>");
                    if ((artHeader == UIHelper.ArtisteerBlockHeader) || (artHeader == UIHelper.ArtisteerBlockHeaderLower))
                    {
                        writer.Write("</div>");
                        if (!useArtisteer3)
                        {
                            writer.Write("</div>");
                        }
                    }
                }
                else
                    if (useJQueryUI)
                    {
                        writer.Write("</div>");
                    }
            }
        }
    }
}
